package com.free.framework.lib.utils;

import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.free.framework.R;
import com.free.framework.base.BaseActivity;

/**
 * @author luchao
 * @time 16/10/28 上午11:55
 */

public class ToolbarHelper {
    private BaseActivity mActivity;
    private Toolbar mToolbar;
    private TextView mTitle;
    private AppBarLayout mAppBarView;
    private FrameLayout mTitleLayout;
    private boolean mIsTitleInCenter;

    public ToolbarHelper(BaseActivity activity) {
        this.mActivity = activity;
    }


    /**
     * 初始化默认布局
     *
     * @param layoutResID 布局资源id
     */
    private void initDefaultLayout(int layoutResID) {
        mAppBarView = (AppBarLayout) LayoutInflater.from(mActivity).inflate(layoutResID, null);
        mToolbar = (Toolbar) mAppBarView.findViewById(R.id.toolBar);
        mTitleLayout = (FrameLayout) mAppBarView.findViewById(R.id.toolbar_group);
        ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView();
        View contentView = ((ViewGroup) decorView.findViewById(android.R.id.content)).getChildAt(0);
        if (null != contentView && contentView instanceof ViewGroup) {
            ViewGroup contentViewGroup = (ViewGroup) contentView;
            if (contentViewGroup instanceof CoordinatorLayout) {
                CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                contentViewGroup.addView(mAppBarView, layoutParams);
            } else {
                LinearLayout parent = new LinearLayout(mActivity);
                parent.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                parent.addView(mAppBarView, layoutParams);
                layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                ViewGroup viewParent = (ViewGroup) contentViewGroup.getParent();
                viewParent.removeView(contentView);
                parent.addView(contentView, layoutParams);
                viewParent.addView(parent);
            }
            mActivity.setSupportActionBar(mToolbar);
            mToolbar.setTitle("");
            AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mTitleLayout.getLayoutParams();
            params.setScrollFlags(
                    AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
        }

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });
    }


    public void attachToActivity(View view, String title, boolean isTitleInCenter) {
        initDefaultLayout(R.layout.fjr_app_bar);
        this.mIsTitleInCenter = isTitleInCenter;
        if (null != view) {
            setTitleLayout(view);
        } else {
            if (isTitleInCenter) {
                setToolbarTitle(title);
            } else {
                mToolbar.setTitle(title);
            }
        }
    }

    private void setTitleLayout(View view) {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = mIsTitleInCenter ? Gravity.CENTER : Gravity.LEFT|Gravity.CENTER_VERTICAL;
        if(!mIsTitleInCenter) {
            layoutParams.leftMargin = mToolbar.getNavigationIcon().getMinimumWidth()*3;
        }
        mTitleLayout.addView(view, layoutParams);
    }

    public void setToolbarTitle(String title) {
        if (null == mTitle) {
            mTitle = new TextView(mActivity);
        }
        mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, 54);
        mTitle.setTextColor(Color.BLACK);
        mTitle.setText(title);
        if (null == mTitle.getParent()) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER;
            mTitleLayout.addView(mTitle, layoutParams);
        }
    }

    public void setTitle(String text) {
        if (mIsTitleInCenter) {
            mTitle.setText(text);
        } else {
            mToolbar.setTitle(text);
        }
    }

    public void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) {
        if (null != mActivity.getSupportActionBar()) {
            mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(showHomeAsUp);
        }
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public void showShadow(boolean show) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (!show) {
                mAppBarView.setElevation(0);
            }
        }
    }
}
